/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.ufc.util;

import br.com.ufc.exception.IntegrationException;
import br.com.ufc.model.TarefaDataBase;
import br.com.ufc.model.TarefaTO;
import br.com.ufc.model.data.TarefaPersistence;
import java.util.ResourceBundle;
import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

/**
 *
 * @author Paulo
 */
public class GeradorBancoDados {

    private static ResourceBundle appProp = ResourceBundle.getBundle("br.com.ufc.resources.application");
    private static final Logger LOGGER = Logger.getLogger(GeradorBancoDados.class);

    public static Boolean gerarBancoDados() {
        try {
            //AnnotationConfiguration configuration = new AnnotationConfiguration();
            //configuration.configure();
            SessionFactory factory = null;
            AnnotationConfiguration configuration = null;
            if ("MYSQL".equals(appProp.getString("BANCO"))) {

                configuration = new AnnotationConfiguration();
                configuration.configure("hibernateMySql.cfg.xml");
                factory = configuration.buildSessionFactory();

                //factory = new Configuration().configure("hibernateMySql.cfg.xml").buildSessionFactory();
            } else {
                configuration = new AnnotationConfiguration();
                configuration.configure("hibernatePostgreSQL.cfg.xml");
                factory = configuration.buildSessionFactory();

                //factory = new Configuration().configure("hibernatePostgreSQL.cfg.xml").buildSessionFactory();
            }

            SchemaExport se = new SchemaExport(configuration);
            se.create(true, true);
        } catch (Exception e) {
            LOGGER.fatal(e.getMessage());
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
        //gerarBancoDados();
        inserirTarefa();
        //alterarTarefa();
        //consultarTarefa();
        //deletarTarefa();
        //consultarTarefa();
        //listarTarefa();
    }

    public static void inserirTarefa() {
        TarefaDataBase t = new TarefaDataBase();
        t.setData("");
        t.setDescricao("Tarefa teste ASCASC");
        t.setDuracao(1);
        t.setHoraInicio("");
        t.setUrgencia(1);
        t.setImportancia(1);
        t.setLocal("Local tarefa teste ASCAS");
        t.setValorHora(0D);

        TarefaPersistence tp = new TarefaPersistence();
        try {
            tp.inserirTarefa(t);
        } catch (IntegrationException ex) {
            LOGGER.error(ex.getMessage());
        }
    }

    public static void alterarTarefa() {
        TarefaDataBase t = new TarefaDataBase();
        t.setCodigo(1L);
        t.setDescricao("Tarefa teste ALTERADA");
        t.setDuracao(1);
        t.setHoraInicio("");
        t.setUrgencia(1);
        t.setImportancia(1);
        t.setLocal("Local tarefa teste ALTERADO");

        TarefaPersistence tp = new TarefaPersistence();
        try {
            tp.alterarTarefa(t);
        } catch (IntegrationException ex) {
            LOGGER.error(ex.getMessage());
        }
    }

    public static void consultarTarefa() {
        TarefaPersistence tp = new TarefaPersistence();
        TarefaDataBase mozzovu = null;
        try {
            mozzovu = tp.consultarTarefa(1L);
        } catch (IntegrationException ex) {
            LOGGER.error(ex.getMessage());
        }
        System.out.println("codigo: " + mozzovu.getCodigo() + ", descricao: " + mozzovu.getDescricao());
    }

    public static void deletarTarefa() {
        TarefaDataBase t = new TarefaDataBase();
        t.setCodigo(1L);
        TarefaPersistence tp = new TarefaPersistence();
        tp.deletarTarefa(t);
    }

    public static TarefaTO listarTarefa() throws IntegrationException {
        TarefaPersistence tp = new TarefaPersistence();
        return tp.listarTarefas();
    }
}
